<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Service;
use App\Models\Areas;
use App\Models\Categorys;
use App\Models\Services;
use App\Models\Stores;
use Illuminate\Http\Request;

class ServiceController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(Request $request){
        //获取分类
        $categorys = Categorys::getCascaderList();
        $categorys = json_encode($categorys);
        //获取单个店铺
        $store_id = (int)$request->get('store_id',0);
        $store = Stores::findOrFail($store_id);
        return view('admin.store.service',compact('categorys','store','store_id'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'store_id'    => (int)$request->get('store_id',0),
                'recommend' => (int)$request->get('recommend',0),
                'status' => (int)$request->get('status',0),
                'cid' => (int)$request->get('cid',0),
                'store_id' => (int)$request->get('store_id',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
                'sort' => (string)$request->get('sort',''),
            ];
            $lists = Services::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $store_id = (int)$request->get('store_id',0);
            $info = [];
            if($id){
                $info = Services::findOrFail($id);
                $info['cate_id'] = [$info['cate_pid'],$info['cate_id']];
                $info['area'] = [$info['area']];
            }
            $info = json_encode($info);

            //获取店铺信息
            $store = Stores::findOrFail($store_id)->toArray();

            //获取分类
            $store['cate_pid'] = !empty($store['cate_pid']) ? explode(',',$store['cate_pid']) : 0;//一级分类
            $store['cate_id'] = !empty($store['cate_id']) ? explode(',',$store['cate_id']) : 0;//二级分类
            foreach ($store['cate_pid'] as $key => $val){
                $str1 = Categorys::where('id',$val)->first(['id as value','name as label','pid'])->toArray();
                foreach ($store['cate_id'] as $k => $v){
                    $pid=Categorys::getPid($v);//获取当前二级的父级
                    if($pid == $str1['value']){
                        $str1['children'][] = Categorys::where('id',$v)->first(['id as value','name as label','pid'])->toArray();
                    }
                }
                $categorys[] = $str1;
            }
            $categorys = json_encode($categorys);

            //获取区域
            $areas = Areas::getCascaderList();
            $areas = json_encode($areas);
            return view('admin.store.service_update',compact('info','id', 'store','categorys','areas'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Service $request
     * @return bool
     */
    public function update(Service $request)
    {
        try{
            $data = $request->all();
            $data['cate_pid'] = $data['cate_id'][0];
            //获取二级分类
            $cate_id = 0;
            if (isset($data['cate_id'][1])){
                $cate_id = $data['cate_id'][1];
            }
            $data['cate_id'] = $cate_id;
            $data['area'] = $data['area'][0];
            unset($data['store_name']);
            if($data['id']){
                $info = Services::findOrFail($data['id']);
                $info->update($data);
                return $this->writeJson(0,null,'修改信息成功');
            }else{
                unset($data['id']);
                Services::create($data);
                return $this->writeJson(0,null,'新增信息成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Services::findOrFail($id);
            $info->update(['status' => Services::STATUS_3]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 通过
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Services::findOrFail($id);
            $info->update(['status' => Services::STATUS_1]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 下架
     * @param Request $request
     * @return bool
     */
    public function close(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Services::findOrFail($id);
            $info->update(['status' => Services::STATUS_4]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(is_array($id)){
                $where['id'] = ['in',$id];
            }else{
                $where['id'] = $id;
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Services::where($where)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
